const service = require("../service/login");
var jwt = require("jsonwebtoken");
const OSS = require("ali-oss"); //oss上传
const {
  v4: uuidv4
} = require("uuid");
const Redis = require("ioredis");
const redis = new Redis();
// 登录
const login = async (ctx) => {
  const res = await service.login(ctx.request.body);
  if (res.length > 0) {
    const token = jwt.sign(ctx.request.body, "secret", {
      expiresIn: "24h"
    });
    const name = res[0].name;
    const id = res[0].id;
    const img = res[0].img;
    const phone = res[0].phone;
    ctx.body = {
      code: 200,
      message: "登录成功",
      data: {
        token,
        name,
        id,
        img,
        phone
      },
    };
  } else {
    ctx.body = {
      code: 403,
      message: "登录失败",
    };
  }
};
// 修改密码
const updatepassword = async (ctx) => {
  const res = await service.updatepassword(ctx.request.body);
  if (res.affectedRows > 0) {
    ctx.body = {
      code: 200,
      message: "密码修改成功",
    };
  } else {
    ctx.body = {
      code: 403,
      message: "密码修改失败",
    };
  }
};

// 修改名字 
const updateName = async (ctx) => {
  const res = await service.updateName(ctx.request.body);
  const ress = await service.findId(ctx.request.body);
  if (res.affectedRows > 0) {
    ctx.body = {
      code: 200,
      message: "名字修改成功",
      data: {
        name: ress[0].name
      }
    };
  } else {
    ctx.body = {
      code: 403,
      message: "名字修改失败",
    };
  }
};
// 验证码
const verificationcode = async (ctx) => {
  const {
    phone1
  } = ctx.request.body; //当条手机号
  //手机号密码
  const res = await service.verificationcode(ctx.request.body);
  if (res.length > 0) {
    let code = await redis.get(phone1);
    if (code == null) {
      let yzm = Math.random().toFixed(6).slice(-6);
      await redis.setex(phone1, 60, yzm);
      const token = jwt.sign(ctx.request.body, "secret", {
        expiresIn: "24h"
      });
      const name = res[0].name;
      const id = res[0].id;
      const img = res[0].img;
      const phone = res[0].phone;
      ctx.body = {
        code: 200,
        message: "验证码发送成功",
        data: {
          token,
          name,
          id,
          img,
          yzm,
          phone
        },
      };
    } else {
      ctx.body = {
        code: 403,
        message: "验证码发送频繁",
      };
    }
  } else {
    ctx.body = {
      code: 403,
      message: "验证码发送错误",
    };
  }
};

// 注册
const register = async (ctx) => {
  const res = await service.login(ctx.request.body);
  if (res.length > 0) {
    ctx.body = {
      code: 403,
      message: "手机号注册频繁",
    };
  } else {
    const res = await service.register(ctx.request.body);
    if (res.affectedRows > 0) {
      ctx.body = {
        code: 200,
        message: "注册成功",
      };
    } else {
      ctx.body = {
        code: 403,
        message: "注册失败",
      };
    }
  }
};

// 上传图片 

const addimg = async (ctx) => {
  const file = ctx.request.files.file;
  const client = new OSS({
    // yourregion填写Bucket所在地域。以华东1（杭州）为例，Region填写为oss-cn-hangzhou。
    region: "oss-cn-beijing",
    // 从环境变量中获取访问凭证。运行本代码示例之前，请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    accessKeyId: "LTAI5tAwd2C51supXvgFaxfr",
    accessKeySecret: "Zm8sfHwExIsXChBCwcVTzubRDidgqz",
    // 填写Bucket名称。
    bucket: "zxsoss",
  });
  try {
    // 填写OSS文件完整路径和本地文件的完整路径。OSS文件完整路径中不能包含Bucket名称。
    // 如果本地文件的完整路径中未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件。
    const result = await client.put(file.originalFilename, file.filepath);
    ctx.body = {
      code: 200,
      message: "上传成功",
      url: result.url,
    };

  } catch (e) {
    console.log(e);
  }
}
// 修改图片 
const updateImg = async (ctx) => {
  const res = await service.updateImg(ctx.request.body);
  if (res.affectedRows > 0) {
    ctx.body = {
      code: 200,
      message: "上传成功",
    };
  } else {
    ctx.body = {
      code: 403,
      message: "上传失败",
    };
  }
}
// 医生数据
const homeall = async (ctx) => {
  const res = await service.homeall(ctx.request.body);
  if (res.length > 0) {
    ctx.body = {
      code: 200,
      message: "查询成功",
      data: res,
    };
  } else {
    ctx.body = {
      code: 403,
      message: "查询失败",
    };
  }
};
// 订单
const orderform = async (ctx) => {
  const res = await service.orderform(ctx.request.body);
  if (res.length > 0) {
    ctx.body = {
      code: 200,
      message: "查询成功",
      data: res,
    };
  } else {
    ctx.body = {
      code: 403,
      message: "查询失败",
    };
  }
};
// 分类
const classification = async (ctx) => {
  const res = await service.classification(ctx.request.body);
  if (res.length > 0) {
    ctx.body = {
      code: 200,
      message: "查询成功",
      data: res,
    };
  } else {
    ctx.body = {
      code: 403,
      message: "查询失败",
    };
  }
};
// 查找详情 id
const addDetails = async (ctx) => {
  const res = await service.addDetails(ctx.request.body);
  if (res.length > 0) {
    ctx.body = {
      code: 200,
      message: "查询成功",
      data: res,
    };
  } else {
    ctx.body = {
      code: 403,
      message: "查询失败",
    };
  }
}

module.exports = {
  login,
  homeall,
  orderform,
  register,
  verificationcode,
  classification,
  updatepassword,
  updateName,
  addimg,
  updateImg,
  addDetails
};